package com.atguigu.orderservice.service.impl;

import com.atguigu.orderservice.client.CourseClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.Order;
import com.atguigu.orderservice.mapper.OrderMapper;
import com.atguigu.orderservice.service.OrderService;
import com.atguigu.orderservice.utils.OrderNoUtil;
import com.atguigu.queryVo.CourseOrderVo;
import com.atguigu.queryVo.UcenterMemberOrderVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, String memberId) {

        //远程调用courseClient
        CourseOrderVo course = courseClient.getCourseInfoOrder(courseId);
        //远程调用ucenterClient
        UcenterMemberOrderVo userInfo = ucenterClient.getUserInfoOrder(memberId);

        //创建Order对象，向order对象里面设置需要数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(course.getTitle());
        order.setCourseCover(course.getCover());
        order.setTeacherName(course.getTeacherName());
        order.setTotalFee(course.getPrice());

        order.setMemberId(memberId);
        order.setNickname(userInfo.getNickname());
        order.setMobile(userInfo.getMobile());

        order.setPayType(1); //支付类型微信
        order.setStatus(0); //未支付

        baseMapper.insert(order);


        return order.getOrderNo();
    }
}
